package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.RoleId;
import com.his.common.Rsp;
import com.his.entity.HisUser;
import com.his.mapper.HisUserMapper;
import com.his.service.HisUserService;
import com.his.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * (HisUser)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:09:49
 */
@Transactional(propagation = Propagation.REQUIRED)
@Service("hisUserService")
public class HisUserServiceImpl implements HisUserService {
    @Resource
    private HisUserMapper hisUserMapper;
    @Resource
    private RoleService roleService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public HisUser queryById(Integer id) {
        return this.hisUserMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param pageParam 实例对象
     * @return 查询的结果
     */
    @Override
    public PageInfo<HisUser> queryAll(PageParam<HisUser> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<>(hisUserMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        System.out.println(pageParam);
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<>(hisUserMapper.queryAll(pageParam.getModel()));
    }

    /**
     * 新增数据
     *
     * @param hisUser 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(HisUser hisUser) {
        if (hisUserMapper.queryByAccount(hisUser.getAccount()) > 0) {
            return Rsp.fail().msg("账号重复");
        }
        if (hisUserMapper.insert(hisUser) > 0) {
            //添加用户的角色
            roleService.addUser(hisUser);
            //添加用户和科室关系
            hisUserMapper.addDepAndUser(hisUser.getId(),hisUser.getDepId());
            return Rsp.success(null);
        }
        return Rsp.fail().msg("添加失败");
    }

    /**
     * 修改数据
     *
     * @param hisUser 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(HisUser hisUser) {
        if(hisUser.getId()==null){
            return Rsp.fail().msg("用户主键不能为空");
        }
        //修改用户角色
        if(hisUser.getRoleId()!=null){
            roleService.updateRoleUser(hisUser.getId(),hisUser.getRoleId());
        }
        //修改用户科室
        if(hisUser.getDepId()!=null){
            hisUserMapper.updateDepUser(hisUser.getId(),hisUser.getDepId());
        }
        return hisUserMapper.update(hisUser) > 0 ? Rsp.success(null) : Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.hisUserMapper.deleteById(id) > 0;
    }

    @Override
    public HisUser login(HisUser user) {
        return hisUserMapper.login(user);
    }

    @Override
    public Rsp getByDepId(Integer id) {
        return Rsp.success(hisUserMapper.getByDepId(id));
    }

    /**
     * 查询医生列表
     * @return
     */
    @Override
    public Rsp selectDoctorList() {
        return Rsp.success(hisUserMapper.selectDoctorList());
    }

    /**
     * 根据科室id和角色id查询医院人士列表
     * @param depId 科室id
     * @param role 角色id
     * @return 医院人员列表
     */
    @Override
    public Rsp getHisUserByDepIdAndRoleId(String depId, RoleId role) {
        return Rsp.success(hisUserMapper.getByDepIdAndRole(depId,role.getRoleId()));
    }
}
